/*
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.fieldscope.api;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Represents a fieldscope schema.
 * 
 * @author Travis Keep
 */
public class Schema {
  
  private final long id;
  private final String name;
  private final String type;
  private final List<FieldGroup> fieldGroups;
  private final String serialized;
  
  /**
   * This ID of this schema as known by fieldscope server.
   */
  public long getId() {
    return id;
  }
  
  /**
   * Name of this schema.
   */
  public String getName() {
    return name;
  }
  
  /**
   * Type of this schema.
   */
  public String getType() {
    return type;
  }
  
  /**
   * Field groups in this schema.
   * Returned list cannot be modified.
   */
  public List<FieldGroup> getFieldGroups() {
    return fieldGroups;
  }
  
  @Override
  public String toString() {
    return String.format("{id=%d, name=%s, type=%s, fieldGroups=%s}", id, name, type, fieldGroups);
  }
  
  /**
   * Serializes the content of this object into a string.
   * @see #unmarshall
   */
  public String marshall() {
    return serialized;
  }
  
  /**
   * Build an object from its string representation as returned by {@link #marshall}.
   * @throws IllegalArgumentException if string representation is invalid.
   */
  public static Schema unmarshall(String s) {
    try {
      return singleFromJSON(new JSONObject(s), s);
    } catch (JSONException e) {
      throw new IllegalArgumentException(e);
    }
  }
  
  static List<Schema> fromJSON(JSONObject json) throws JSONException {
    JSONArray results = json.getJSONArray("results");
    List<Schema> result = new ArrayList<Schema>(results.length());
    for (int i = 0; i < results.length(); i++) {
      JSONObject schemaJSON = results.getJSONObject(i);
      result.add(singleFromJSON(schemaJSON, schemaJSON.toString()));
    }
    return result;
  }
  
  private Schema(long id, String name, String type, List<FieldGroup> fieldGroups, String serialized) {
    this.id = id;
    this.name = name;
    this.type = type;
    this.fieldGroups = fieldGroups;
    this.serialized = serialized;
  }
  
  private static Schema singleFromJSON(JSONObject jsonObject, String serialized) throws JSONException {
    long id = jsonObject.getLong("id");
    String name = jsonObject.getString("name");
    String type = jsonObject.getString("type");
    JSONArray fieldGroupJSON = jsonObject.getJSONArray("field_groups");
    return new Schema(
        id,
        name,
        type,
        Collections.unmodifiableList(FieldGroup.fromJSON(fieldGroupJSON)),
        serialized);
  }
}
